package com.ecmap.hongyun.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ecmap.hongyun.commons.utils.StringUtils;
import com.ecmap.hongyun.commons.utils.UserUtil;
import com.ecmap.hongyun.model.User;
import com.ecmap.hongyun.service.IUserService;
import com.ecmap.hongyun.web.form.PasswordForm;
import com.ecmap.hongyun.web.form.UserForm;
import com.google.gson.Gson;
/**
 * 
 * @className:UserController.java
 * @classDescription:用户控制器
 * @author: DANIEL DENG
 * @createTime: 2012-8-13
 *
 */
@Controller
public class UserController {
	
	@Resource
	private IUserService userService ;
	
	/**
	 * 登录
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping( value="/login", method =RequestMethod.POST)
	public String login(@ModelAttribute UserForm userForm, ModelMap modelMap , HttpServletRequest request, HttpServletResponse response ){
		String account = userForm.getAccount();
		String password = userForm.getPassword();
		if( StringUtils.isNotEmpty( account ) &&  StringUtils.isNotEmpty( password ) ){
			User user = null;
			try {
				user = userService.findUserByUsername(account) ;
			} catch (Exception e) {
			}
			if(user !=null && StringUtils.getMD5(password).equals( user.getPassword() )){		
				UserUtil.setUserInSession(request, user);
				return "redirect:/index";
			}else{
				//4:账号或密码不正确
				modelMap.put("t", "4");
				return "redirect:/to-login";
			}	
		}else{
			//9:账号或密码不能为空
			modelMap.put("t", "9");
			return "redirect:/to-login";
		}
	}
	
	/**
	 * 直接跳到登录页面
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping( value="/to-login", method =RequestMethod.GET)
	public String toLogin( ModelMap modelMap , HttpServletRequest request ){
		//modelMap.put("userType", InitSetting.getValueByKey( Constants.SMS_USER_TYPE ) );
		return "/user/login";
	}
	
	/**
	 * 直接跳到注册页面
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping( value="/to-register", method =RequestMethod.GET)
	public String toRegister( ModelMap modelMap , HttpServletRequest request ){
		
		return "/user/register";
	}
	
	
	/**
	 * 跳转到修改密码的界面
	 * @param request
	 * @return
	 */
	@RequestMapping( value="/to-modify-password",method =RequestMethod.GET)
	public String toFindPassword(HttpServletRequest request){
		
		return "/user/modify_password";         
	}	
	

	/**
	 * 修改密码
	 * @param passwordForm
	 * @param request
	 * @return
	 */
	@RequestMapping( value="/modify-password", method =RequestMethod.POST)
	@ResponseBody
	public String modifyPwd( @ModelAttribute PasswordForm passwordForm,HttpServletRequest request ){
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		Gson gson = new Gson();
		
		String password = passwordForm.getPassword();
		String newpassword = passwordForm.getNewpassword();
		String repassword = passwordForm.getRepassword();
		if( StringUtils.isNotEmpty( password ) &&  StringUtils.isNotEmpty( newpassword )&&  StringUtils.isNotEmpty( repassword ) ){
			User user = UserUtil.getUserFromSession(request);
			if( user != null ){
				try{
					User u = userService.findUserById( user.getId() );
					u.setUpdateTime(new Date());
					u.setPassword( StringUtils.getMD5( newpassword ) ) ;
					userService.updateUser(u);
					
						
					result.put("code", 0);
					result.put("msg", "密码修改成功");
					return gson.toJson(result);
				}catch(Exception e){
					result.put("code", 9);
					result.put("msg", "密码修改失败");
					return gson.toJson(result);
				}
			}else{
				result.put("code", 9);
				result.put("msg", "登录超时，请重新登录");
				return gson.toJson(result);
			}
		}else{
			result.put("code", 9);
			result.put("msg", "任何一项都不能为空");
			return gson.toJson(result);
		}
		
	}
	
	
	@RequestMapping( value="/logout",method=RequestMethod.GET)
	public String userExit(ModelMap modelMap, HttpServletRequest request , HttpServletResponse response ){
		UserUtil.logout(request,response);
		return "/user/login";
	}
	
	

}
